package com.bhs.sansonglogistics.ui.enterprise;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.ArrayMap;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.MyApplication;
import com.bhs.sansonglogistics.bean.AdvertisingBean;
import com.bhs.sansonglogistics.bean.BusinessAdministrationBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.consumptionCoupon.CouponManagementActivity;
import com.bhs.sansonglogistics.ui.consumptionCoupon.IssueCouponsActivity;
import com.bhs.sansonglogistics.ui.quality.MemberActivity;
import com.bhs.sansonglogistics.ui.quality.PrivateLinePromotionActivity;
import com.bhs.sansonglogistics.ui.quality.QualityVoucherActivity;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.bhs.sansonglogistics.view.banner.AutoScrollViewPager;
import com.bhs.sansonglogistics.view.banner.BannerIndicator;
import com.bhs.sansonglogistics.view.banner.ViewPagerAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 企业管理
 */
public class BusinessAdministrationActivity extends BaseActivity implements View.OnClickListener, NetCallBack {

    private AutoScrollViewPager mVpCarousel;
    private BannerIndicator mIndicator;
    private TextView mTvEnterStatus;//入驻状态

    private TextView mTvLine;//线路条数
    private TextView mTvPaymentStatus;//缴纳状态
    private RelativeLayout rlCompanyDetails;

    @Override
    public int initLayout() {
        return R.layout.activity_business_ddministration;
    }

    @Override
    public void initView() {
        mVpCarousel = findViewById(R.id.vp_carousel);
        mIndicator = findViewById(R.id.indicator);
        findViewById(R.id.rl_enterprise_settlement).setOnClickListener(this);
        mTvEnterStatus = findViewById(R.id.tv_enter_status);
        rlCompanyDetails = findViewById(R.id.rl_company_details);
        rlCompanyDetails.setOnClickListener(this);
        findViewById(R.id.rl_dedicated_line_management).setOnClickListener(this);
        mTvLine = findViewById(R.id.tv_line);
        findViewById(R.id.rl_company_video).setOnClickListener(this);
        findViewById(R.id.rl_quality_special_line).setOnClickListener(this);
        mTvPaymentStatus = findViewById(R.id.tv_payment_status);
        findViewById(R.id.rl_member_line).setOnClickListener(this);
        findViewById(R.id.rl_private_line_promotion).setOnClickListener(this);
        RelativeLayout rl_coupon_management = findViewById(R.id.rl_coupon_management);
        rl_coupon_management.setOnClickListener(this);

    }

    @Override
    public void initData() {
        Map<String, String> map = new ArrayMap<>();
        map.put("platform", "logistics");
        map.put("city", "1");
        map.put("ad_sign", "AppLogisticsManage");//AppLogisticsManage
        networkRequest(netApi.get_ad_list(map), this, 9527);

    }

    @Override
    public void onResume() {
        super.onResume();
        if (mVpCarousel != null) {
            mVpCarousel.startAutoPlay();
        }
        networkRequest(netApi.getIndexInfo(), this);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mVpCarousel != null) {
            mVpCarousel.stopAutoPlay();
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //企业入驻
            case R.id.rl_enterprise_settlement:
                startActivity(new Intent(BusinessAdministrationActivity.this, EnterpriseSettlementActivity.class));
                break;
            //公司详情
            case R.id.rl_company_details:
                startActivity(new Intent(BusinessAdministrationActivity.this, CompanyDetailsActivity.class));
                break;
            //专线管理
            case R.id.rl_dedicated_line_management:
                startActivity(new Intent(BusinessAdministrationActivity.this, DedicatedLineManagementActivity.class));
                break;
            //企业视频
            case R.id.rl_company_video:
                startActivity(new Intent(mActivity, IssueVideoActivity.class));
                break;
            //品质专线
            case R.id.rl_quality_special_line:
                startActivity(new Intent(mActivity, QualityVoucherActivity.class));
                break;
            //会员专线
            case R.id.rl_member_line:
                startActivity(new Intent(mActivity, MemberActivity.class));
                break;
            //专线推广
            case R.id.rl_private_line_promotion:
                startActivity(new Intent(mActivity, DedicatedLineManagementActivity.class)
                        .putExtra("type", 2));
                break;
            //发券管理
            case R.id.rl_coupon_management:
                startActivity(new Intent(mActivity, CouponManagementActivity.class));
                break;
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 9527) {
            AdvertisingBean infoBean = new Gson().fromJson(json, AdvertisingBean.class);
            if (infoBean.isStatus() && !MyUtils.isEmpty(infoBean.getData().getList())) {
                ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(infoBean.getData().getList(), mContext,false,true);
                mVpCarousel.setAdapter(pagerAdapter);
                mIndicator.setUpWidthViewPager(mVpCarousel, infoBean.getData().getList().size());
            }
        } else {
            BusinessAdministrationBean bean = new Gson().fromJson(json, BusinessAdministrationBean.class);
            if (bean.isStatus()) {
                mTvLine.setText(String.format("%s条", bean.getData().getLine_num()));
                SharedPreferencesUtils.saveString(mActivity, "deposit_status", bean.getData().getCompany_info().getDeposit_status());
                if (bean.getData().getCompany_info().getPark_id() > 0) {
                    mTvEnterStatus.setText("已入驻");
                    rlCompanyDetails.setVisibility(View.VISIBLE);
                } else {
                    mTvEnterStatus.setText("未入驻");
                    rlCompanyDetails.setVisibility(View.INVISIBLE);
                }
            } else {
                ToastUtil.show(bean.getMsg());
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}